package com.neusoft.elmboot.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.elmboot.po.PlatformRevenue;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Mapper
public interface BusinessWalletMapper extends BaseMapper<PlatformRevenue> {

    /**
     * 获取商家钱包信息
     */
    @Select("SELECT " +
            "vw.walletId as businessId, " +
            "b.businessName, " +
            "vw.balance as wallet_balance, " +
            "vw.frozenAmount as frozen_amount, " +
            "(vw.balance - vw.frozenAmount) as available_balance, " +
            "COALESCE(stats.total_orders, 0) as total_orders, " +
            "COALESCE(stats.total_revenue, 0) as total_revenue, " +
            "COALESCE(stats.total_platform_fee, 0) as total_platform_fee, " +
            "vw.createTime as wallet_create_time, " +
            "vw.updateTime as wallet_update_time " +
            "FROM virtualwallet vw " +
            "LEFT JOIN business b ON vw.walletId = b.businessId " +
            "LEFT JOIN ( " +
            "    SELECT business_id, " +
            "           COUNT(*) as total_orders, " +
            "           SUM(business_received) as total_revenue, " +
            "           SUM(platform_fee) as total_platform_fee " +
            "    FROM platform_revenue " +
            "    WHERE business_id = #{businessId} " +
            "    GROUP BY business_id " +
            ") stats ON vw.walletId = stats.business_id " +
            "WHERE vw.walletId = #{businessId}")
    Map<String, Object> getBusinessWalletInfo(@Param("businessId") String businessId);

    /**
     * 获取所有商家钱包信息
     */
    @Select("SELECT " +
            "vw.walletId as businessId, " +
            "b.businessName, " +
            "vw.balance as wallet_balance, " +
            "vw.frozenAmount as frozen_amount, " +
            "(vw.balance - vw.frozenAmount) as available_balance, " +
            "COALESCE(stats.total_orders, 0) as total_orders, " +
            "COALESCE(stats.total_revenue, 0) as total_revenue, " +
            "COALESCE(stats.total_platform_fee, 0) as total_platform_fee, " +
            "vw.createTime as wallet_create_time, " +
            "vw.updateTime as wallet_update_time " +
            "FROM virtualwallet vw " +
            "LEFT JOIN business b ON vw.walletId = b.businessId " +
            "LEFT JOIN ( " +
            "    SELECT business_id, " +
            "           COUNT(*) as total_orders, " +
            "           SUM(business_received) as total_revenue, " +
            "           SUM(platform_fee) as total_platform_fee " +
            "    FROM platform_revenue " +
            "    GROUP BY business_id " +
            ") stats ON vw.walletId = stats.business_id " +
            "WHERE b.businessId IS NOT NULL " +
            "ORDER BY vw.walletId")
    List<Map<String, Object>> getAllBusinessWalletInfo();

    /**
     * 调用存储过程处理订单支付
     */
    void processOrderPayment(@Param("userId") String userId,
                           @Param("businessId") String businessId,
                           @Param("orderId") Integer orderId,
                           @Param("amount") BigDecimal amount,
                           @Param("platformFee") BigDecimal platformFee,
                           @Param("transactionId") String transactionId,
                           @Param("resultCode") Integer resultCode,
                           @Param("resultMessage") String resultMessage);

    /**
     * 获取商家收益统计
     */
    @Select("SELECT " +
            "COUNT(*) as orderCount, " +
            "COALESCE(SUM(order_amount), 0) as totalOrderAmount, " +
            "COALESCE(SUM(business_received), 0) as totalReceived, " +
            "COALESCE(SUM(platform_fee), 0) as totalPlatformFee " +
            "FROM platform_revenue " +
            "WHERE business_id = #{businessId} " +
            "AND DATE(create_time) = CURDATE()")
    Map<String, Object> getTodayRevenueStats(@Param("businessId") String businessId);

    /**
     * 获取商家月度收益统计
     */
    @Select("SELECT " +
            "COUNT(*) as orderCount, " +
            "COALESCE(SUM(order_amount), 0) as totalOrderAmount, " +
            "COALESCE(SUM(business_received), 0) as totalReceived, " +
            "COALESCE(SUM(platform_fee), 0) as totalPlatformFee " +
            "FROM platform_revenue " +
            "WHERE business_id = #{businessId} " +
            "AND YEAR(create_time) = YEAR(CURDATE()) " +
            "AND MONTH(create_time) = MONTH(CURDATE())")
    Map<String, Object> getMonthlyRevenueStats(@Param("businessId") String businessId);

    /**
     * 获取平台总收益统计
     */
    @Select("SELECT " +
            "COUNT(*) as totalOrders, " +
            "COALESCE(SUM(order_amount), 0) as totalOrderAmount, " +
            "COALESCE(SUM(business_received), 0) as totalBusinessReceived, " +
            "COALESCE(SUM(platform_fee), 0) as totalPlatformFee " +
            "FROM platform_revenue")
    Map<String, Object> getPlatformTotalStats();

    /**
     * 获取商家钱包交易记录
     */
    @Select("SELECT " +
            "wt.trans_id, wt.from_wallet_id, wt.to_wallet_id, wt.amount, " +
            "wt.transaction_type, wt.description, wt.platform_fee, wt.create_time " +
            "FROM wallettransaction wt " +
            "WHERE (wt.from_wallet_id = #{businessId} OR wt.to_wallet_id = #{businessId}) " +
            "ORDER BY wt.create_time DESC " +
            "LIMIT #{limit} OFFSET #{offset}")
    List<Map<String, Object>> getBusinessTransactions(@Param("businessId") String businessId,
                                                     @Param("limit") Integer limit,
                                                     @Param("offset") Integer offset);
}
